package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.service.WechatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WechatServiceImpl implements WechatService {

    // 登录
    private static final String REQUEST_URL = "https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code";
    // 获取token
    private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
    // 获取手机号
    private static final String PHONE_REQUEST_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";
    @Value("${wechat.AppId}")
    private String appid;
    @Value("${wechat.AppSecret}")
    private String secret;

    /**
     * 获取openId
     *
     * @param code 前端传递来的code
     * @return openId
     */
    @Override
    public String getOpenId(String code) {
        Map<String, Object> paramMap  = new HashMap<>();
        paramMap .put("appid", appid);
        paramMap .put("secret", secret);
        paramMap .put("js_code", code);
        //获取响应结果
        String result = HttpUtil.get(REQUEST_URL, paramMap);
        //将json字符串转成json对象
        JSONObject jsonObject = JSONUtil.parseObj(result);
        //判断错误码是否为空
        if(ObjectUtil.isNotEmpty(jsonObject.get("errcode"))){
            //错误码为空 抛出错误信息
            throw new BaseException(jsonObject.getStr("errmsg"));
        }
        //获取openId 并返回
        return jsonObject.getStr("openid");
    }

    /**
     * 获取手机号
     *
     * @param detailCode
     * @return 手机号
     */
    @Override
    public String getPhone(String detailCode) {
        //获取手机号之前先通过创建方法 调用微信接口获取token
        String token = getToken();
        Map<String, Object> paramMap = new HashMap<>();

        paramMap.put("code", detailCode);
        String result = HttpUtil.post(PHONE_REQUEST_URL + token, JSONUtil.toJsonStr(paramMap));
        //奖结果转换成json对象
        JSONObject jsonObject = JSONUtil.parseObj(result);
        //判断响应到的errcode是否为0  不为0则错误  通过错误码判断
        if(!jsonObject.getInt("errcode").equals(0)){
            throw new BaseException(jsonObject.getStr("errmsg"));
        }
        //得到手机号并返回
        return jsonObject.getJSONObject("phone_info").getStr("phoneNumber");
    }

    private String getToken() {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid", appid);
        paramMap.put("secret", secret);
        //获取token
        String result = HttpUtil.get(TOKEN_URL, paramMap);
        //奖结果转换成json对象
        JSONObject jsonObject = JSONUtil.parseObj(result);
        //判断是否为空  通过错误码判断
        if(ObjectUtil.isNotEmpty(jsonObject.getInt("errcode"))){
            throw new BaseException(jsonObject.getStr("errmsg"));
        }
        //获取token并返回
        return jsonObject.getStr("access_token");
    }
}
